package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.InvoiceRegisterDao;
import com.woniuxy.entity.Invoice_register;
import com.woniuxy.entity.OrderEntity;
import com.woniuxy.entity.QueryEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.InvoiceRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Leimin
 * @date 2022/04/26 19:20
 **/
@Service
public class InvoiceRegisterServiceImpl implements InvoiceRegisterService {
    @Resource
    InvoiceRegisterDao invoiceRegisterDao;
    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Override
    public ResponseEntity findAllByCondition(QueryEntity<Invoice_register> queryEntity) {
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());

        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }

        List<Invoice_register> list = invoiceRegisterDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Invoice_register> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findAll() {
        List<Invoice_register> list = invoiceRegisterDao.findAll();
        return new ResponseEntity<>(200,"成功",list);
    }

    @Override
    public ResponseEntity findOne(int id) {
        Invoice_register invoice_register = invoiceRegisterDao.findOne(id);
        return new ResponseEntity(200,"成功",invoice_register);
    }

    @Transactional
    @Override
    public ResponseEntity addOne(Invoice_register invoice_register) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("AddInvoice");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try{
            // 新增发票
            int index1 = invoiceRegisterDao.addOne(invoice_register);
            // 更改订单状态
            int index2 = invoiceRegisterDao.updateOrder(invoice_register.getOrder_num(),3);
            if (index1>0 && index2>0){
                return ResponseEntity.SUCCESS;
            }
            return ResponseEntity.Fail;
        }catch (Exception ex){
            transactionManager.rollback(status);
            throw ex;
        }
    }

    @Transactional
    @Override
    public ResponseEntity deleteOne(int id) {
        //获取订单号
        Invoice_register old = invoiceRegisterDao.findOne(id);
        //删除发票
        invoiceRegisterDao.deleteOne(id);
        //更改订单状态
        invoiceRegisterDao.updateOrder(old.getOrder_num(),2);

        return ResponseEntity.SUCCESS;
    }

    @Transactional
    @Override
    public ResponseEntity deleteBatch(int[] ids) {
        //查找ids对应的订单号
        List<String> order_nums = invoiceRegisterDao.findBatchOrder_num(ids);
        //删除发票
        invoiceRegisterDao.deleteBatch(ids);
        //更改订单状态
        invoiceRegisterDao.updateBatchOrder(order_nums);

        return ResponseEntity.SUCCESS;
    }

    @Transactional
    @Override
    public ResponseEntity updateOne(Invoice_register invoice_register) {
        //获取以前的订单号
        Invoice_register old = invoiceRegisterDao.findOne(invoice_register.getId());
        //更改发票表
        int index = invoiceRegisterDao.updateOne(invoice_register);
        //更改订单表：原订单号状态改为2，新订单号状态改为3
        invoiceRegisterDao.updateOrder(old.getOrder_num(),2);
        invoiceRegisterDao.updateOrder(invoice_register.getOrder_num(),3);
        return ResponseEntity.SUCCESS;
    }

    @Override
    public ResponseEntity findOrder(QueryEntity<OrderEntity> queryEntity) {
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        List<OrderEntity> list = invoiceRegisterDao.findOrder(queryEntity.getEntity());
        PageInfo<OrderEntity> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }
}

